package com.grayson.wificontrol;

import android.app.Activity;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;

public class WificontrolActivity extends Activity 
{
	private RadioButton     _RdoWifiAlways;
	private RadioButton     _RdoWifiOff;
	private RadioButton     _RdoWifiPlugged;
	private RadioGroup      _RdoWifiPolicy;
	private ToggleButton    _TglWifi;
	private WifiManager     _WifiManager;	

	/**
	 * 
	 */
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    	initializeComponents();
    }
    
    /**
     * 
     */
    public void onResume()
    {
    	setSleepPolicyToggle();
    	setWifiToggle();
    	
    	super.onResume();
    }
        
    /**
     * 
     */
    private void initializeComponents() 
    {
        _RdoWifiAlways        = (RadioButton)  findViewById(R.id.radio_always);
        _RdoWifiOff           = (RadioButton)  findViewById(R.id.radio_off);
        _RdoWifiPlugged       = (RadioButton)  findViewById(R.id.radio_plugged);
        _RdoWifiPolicy        = (RadioGroup)   findViewById(R.id.radio_groupPolicy);
        _TglWifi              = (ToggleButton) findViewById(R.id.toggle_wifi);
        _WifiManager          = (WifiManager)  getSystemService(WIFI_SERVICE);

    	_RdoWifiAlways.setOnClickListener(radio_listener);
    	_RdoWifiOff.setOnClickListener(radio_listener);
    	_RdoWifiPlugged.setOnClickListener(radio_listener);
    	_TglWifi.setOnClickListener(toggle_listener);
    	
    	setSleepPolicyToggle();
    	setWifiToggle();
    };
    
    /**
     * 
     */
    private void setSleepPolicyToggle() 
    {
    	int currentValue = 0;
		try {
			currentValue = Settings.System.getInt(getContentResolver(),
			        Settings.System.WIFI_SLEEP_POLICY);
		} catch (SettingNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    	switch(currentValue)
    	{
    	case Settings.System.WIFI_SLEEP_POLICY_DEFAULT:
    		_RdoWifiPolicy.check(_RdoWifiOff.getId());
    		break;
    	case Settings.System.WIFI_SLEEP_POLICY_NEVER:
    		_RdoWifiPolicy.check(_RdoWifiAlways.getId());
    		break;
    	case Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED:
    		_RdoWifiPolicy.check(_RdoWifiPlugged.getId());
    		break;
    	default:
    		Toast.makeText(WificontrolActivity.this, 
    				       "Unknown error: Unable to get current WiFi sleep policy", 
    				       Toast.LENGTH_SHORT).show();
    		break;
    	}
    };
    
    /**
     * 
     */
    private void setWifiToggle() 
    {
    	if(_WifiManager.isWifiEnabled())
        {
        	_TglWifi.setChecked(true);	
        }
        else
        {
        	_TglWifi.setChecked(false);
        }
    };
        
    /**
     * 
     */
    private OnClickListener radio_listener = new OnClickListener() 
    {
        public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            
            if(rb.getText() == getString(R.string.wifialways))
            {
            	Settings.System.putInt(getContentResolver(),
            			Settings.System.WIFI_SLEEP_POLICY, 
            			Settings.System.WIFI_SLEEP_POLICY_NEVER);
            }
            else if(rb.getText() == getString(R.string.wifiplugged))
            {
            	Settings.System.putInt(getContentResolver(),
            			Settings.System.WIFI_SLEEP_POLICY, 
          			  	Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED);
            }
            else if(rb.getText() == getString(R.string.wifisleep))
            {            	
            	Settings.System.putInt(getContentResolver(),
          			  	Settings.System.WIFI_SLEEP_POLICY, 
          			  	Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
            }
            
            Toast.makeText(WificontrolActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
        }
    };
    
    /**
     * 
     */
    private OnClickListener toggle_listener = new OnClickListener() {
    	public void onClick(View v) {
    		ToggleButton tb = (ToggleButton) v;
    		if(tb.isChecked())
    		{
    			_WifiManager.setWifiEnabled(true);
    		}
    		else
    		{
    			_WifiManager.setWifiEnabled(false);
    		}
    		Toast.makeText(WificontrolActivity.this, tb.getText(), Toast.LENGTH_SHORT).show();
    	}
    };
}